package com.me.components.fix {
	import com.me.components.MapResource;
	import com.me.obj.Building;
	import com.me.utils.StringUtils;

	import flash.events.Event;
	import flash.events.MouseEvent;

	import spark.components.Label;
	import spark.components.Panel;
	import spark.components.RadioButton;
	import spark.components.RadioButtonGroup;
	import spark.components.TextInput;
	import spark.events.TextOperationEvent;

	/**
	 *
	 * @author yw
	 *
	 * @Date 2011-9-19 下午02:11:39
	 */
	public class MapResourceProperty extends Panel {
		private static var _instance:MapResourceProperty;

		private var _group:RadioButtonGroup;

		private var xInput:TextInput;

		private var yInput:TextInput;

		private var idInput:TextInput;

		private var radioBuilding:RadioButton;

		private var radioNpc:RadioButton;

		public static function getInstance():MapResourceProperty {
			if (_instance == null) {
				_instance = new MapResourceProperty(new SingletonEnforce());
			}
			return _instance;
		}

		public function MapResourceProperty(enforce:SingletonEnforce) {
			if (enforce == null) {
				throw new Error("MapResourceProperty can not be intance");
			}
			width = 700;
			height = 175;
		}

		override protected function createChildren():void {
			super.createChildren();
			var l:Label = new Label();
			l.width = 100;
			l.text = "类型";
			l.x = 11;
			l.y = 13;
			addElement(l);

			l = new Label();
			l.width = 100;
			l.text = "ID,坐标";
			l.x = 11;
			l.y = 65;
			addElement(l);

			l = new Label();
			l.width = 100;
			l.text = "X";
			l.x = 33;
			l.y = 95;
			addElement(l);

			l = new Label();
			l.width = 100;
			l.text = "Y";
			l.x = 214;
			l.y = 95;
			addElement(l);

			l = new Label();
			l.width = 100;
			l.text = "ID";
			l.move(395, 95);
			addElement(l);

			xInput = new TextInput();
			xInput.x = 51;
			xInput.y = 88;
			xInput.text = "0";
			xInput.restrict = '0-9';
			xInput.name = "X";
			xInput.addEventListener(TextOperationEvent.CHANGE, changeHandler);
			addElement(xInput);

			yInput = new TextInput();
			yInput.x = 234;
			yInput.y = 88;
			yInput.name = "Y";
			yInput.text = "0";
			yInput.restrict = '0-9';
			yInput.addEventListener(TextOperationEvent.CHANGE, changeHandler);
			addElement(yInput);

			idInput = new TextInput();
			idInput.x = 417;
			idInput.y = 88;
			idInput.name = "ID";
			idInput.text = "0";
			idInput.restrict = '0-9';
			idInput.addEventListener(TextOperationEvent.CHANGE, changeHandler);
			addElement(idInput);


			_group = new RadioButtonGroup();
			_group.addEventListener(Event.CHANGE, selectChangeHandler);

			radioBuilding = new RadioButton();
			radioBuilding.x = radioBuilding.y = 34;
			radioBuilding.label = "建筑";
			radioBuilding.group = _group;
			radioBuilding.name = Building.TYPE_BUILDING;
			radioBuilding.selected = true;
			addElement(radioBuilding);

			radioNpc = new RadioButton();
			radioNpc.y = 34;
			radioNpc.x = 117;
			radioNpc.label = "NPC";
			radioNpc.group = _group;
			radioNpc.name = Building.TYPE_NPC;
			addElement(radioNpc);
		}

		override protected function childrenCreated():void {
			super.childrenCreated();
			title = "属性";
//			titleDisplay.addEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
		}

		private function startDragHandler(e:MouseEvent):void {
			startDrag(false);
			stage.addEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
		}

		private function stopDragHandler(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
			stopDrag();
		}

		private function changeHandler(e:TextOperationEvent):void {
			if (_resource == null) {
				return;
			}
			var target:String = e.currentTarget.name as String;
			if ("X" == target) {
				_resource.x = StringUtils.getInteger(e.currentTarget.text);
				_resource.attatchment.x = _resource.x;
			} else if ("Y" == target) {
				_resource.y = StringUtils.getInteger(e.currentTarget.text);
				_resource.attatchment.y = _resource.y;
			} else if ("ID" == target) {
				_resource.attatchment.id = idInput.text;
			}
			MapResourceLayer.getInstance().spritePropertyChanged(_resource.fileName);
			propertychange();
		}

		public function set selectedResource(res:MapResource):void {
			_resource = res;
			yInput.text = (_resource) ? "" + _resource.y : "0";
			xInput.text = (_resource) ? "" + _resource.x : "0";
			if (res && res.attatchment.btype == Building.TYPE_BUILDING) {
				radioBuilding.selected = true;
			} else {
				radioNpc.selected = true;
			}
			propertychange();
		}

		private function selectChangeHandler(e:Event):void {
			if (_resource == null) {
				return;
			}
			_resource.attatchment.btype = (e.currentTarget as RadioButtonGroup).selection.name;
			MapResourceLayer.getInstance().spritePropertyChanged(_resource.fileName);
			propertychange();
		}

		private function propertychange():void {
			if (_resource) {
				title = "属性[" + _resource.attatchment.desc + "]";
			}
		}

		private var _resource:MapResource;
	}
}

class SingletonEnforce {

}